import { getList, getByList } from "../../api/api.js"

Page({
  /**
   * 页面的初始数据
   */
  data: {
    start: 0,
    num: 20,
    load: true,
    loading: true
  },
  onEnterDetail: function (event) {
    var id = event.currentTarget.dataset.id;
    my.navigateTo({
      url: '/pages/detail/detail?id=' + id
    });
  },
  onLoadMore: function (event) {
    var that = this;

    if (this.data.type == 'id' && this.data.load && this.data.loading) {
      this.setData({
        loading: false
      });
      /*wx.showLoading({
        title: '加载中...',
      })*/
      getByList(this.data.id, this.data.list.length).then(res => {
        this.callBack(res)
      })
    }
  },

  /**
   * 回调
   */
  callBack: function (ret) {
    if (ret.status == 0 && ret.result.list.length == 20) {
      this.setData({
        'list': this.data.list.concat(ret.result.list)
      });
      this.setData({
        loading: true
      });
    } else {
      this.setData({
        "load": false,
        loading: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    my.showLoading({
      title: '加载中...'
    });
    var id = options.id?options.id:165;
    var search = options.search;
    var name = options.name?options.name:'肺炎';
    var that = this;
    if (id && !search) {
      this.setData({
        'type': 'id'
      });
      this.setData({
        'id': id
      });
      my.setNavigationBar({
        title: name
      });
      getByList(id, 0).then(res => {
        this.loadCallBack(res)
      })
    }

    if (search) {
      my.setNavigationBar({
        title: search
      });
      this.setData({
        'load': false
      });
      getList(search).then(res => {
        this.loadCallBack(res)
      })
    }
  },

  /**
   * load回调
   */
  loadCallBack: function (ret) {
    my.hideLoading();

    if (ret.status == 0) {
      this.setData({
        'list': ret.result.list
      });
    } else {
      setTimeout(function () {
        my.showToast({
          content: '没有信息'
        });
      }, 1000);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  enterNcov: function(){
    my.navigateToMiniProgram({
      appId: '2021001132600368',
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});